package it.gameplanet.client.widgets.articolo;

import static com.google.gwt.user.client.ui.DockPanel.CENTER;
import static com.google.gwt.user.client.ui.DockPanel.SOUTH;
import it.gameplanet.client.widgets.text.RichTextToolbar;
import it.gameplanet.model.Articolo;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class AbstractDettaglioArticolo extends Composite {

	private class MyTextBox extends TextBox  {

		public MyTextBox(String valore) {
			super();
			
			setText(valore);
		}
		
		public MyTextBox(double valore) {
			this(Double.toString(valore));
		}
		
		public MyTextBox(int valore) {
			this(Integer.toString(valore));
		}
	}
	
	public AbstractDettaglioArticolo(final Articolo articolo) {
		super();

		StackPanel stackPanel = new StackPanel();
		
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Nome:</b>"));
		final MyTextBox nome = new MyTextBox(articolo.getNome());
		dialogVPanel.add(nome);
		dialogVPanel.add(new HTML("<b>Descrizione:</b>"));
		
		final RichTextArea descrizione = new RichTextArea();
		descrizione.ensureDebugId("cwRichText-area");
		descrizione.setSize("100%", "14em");
	    RichTextToolbar toolbarDescrizione = new RichTextToolbar(descrizione);
	    toolbarDescrizione.ensureDebugId("cwRichText-toolbar");
	    toolbarDescrizione.setWidth("100%");

	    // Add the components to a panel
	    Grid gridDescrizione = new Grid(2, 1);
	    gridDescrizione.setStyleName("cw-RichText");
	    gridDescrizione.setWidget(0, 0, toolbarDescrizione);
	    gridDescrizione.setWidget(1, 0, descrizione);

		descrizione.setHTML(articolo.getDescrizione());
		dialogVPanel.add(gridDescrizione);
		
		dialogVPanel.add(new HTML("<b>Categoria:</b>"));
		final MyTextBox categoria = new MyTextBox(articolo.getCategoria());
		categoria.setReadOnly(true);
		categoria.setEnabled(false);
		dialogVPanel.add(categoria);

		Grid grid = new Grid(2,2);
		grid.setWidget(0, 0, new HTML("<b>Prezzo:</b>"));
		grid.setWidget(0, 1, new HTML("<b>Quantit&agrave;:</b>"));
		grid.setWidth("100%");
		
		final MyTextBox prezzo = new MyTextBox(articolo.getPrezzo());
		prezzo.setVisibleLength(5);
		grid.setWidget(1, 0, prezzo);

		final MyTextBox quantita = new MyTextBox(articolo.getQuantita());
		quantita.setVisibleLength(4);
		grid.setWidget(1, 1, quantita);
		
		dialogVPanel.add(grid);
		
		dialogVPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		dialogVPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		
		Button editButton = new Button(getButtonLabel());
		// Add a handler to close the DialogBox
		editButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				articolo.setNome(nome.getText());
				articolo.setDescrizione(descrizione.getHTML());
//				articolo.setCategoria(categoria.getText());
				articolo.setPrezzo(Double.parseDouble(prezzo.getText()));
				articolo.setQuantita(Integer.parseInt(quantita.getText()));
				
				callRemoteAsyncService(articolo);
			}
		});

		DockPanel containerPanel = new DockPanel();
		containerPanel.add(dialogVPanel, CENTER);
		containerPanel.add(editButton, SOUTH);
		containerPanel.addStyleName("gwt-FullWidthAndHeight");
				
		stackPanel.add(containerPanel, articolo.getNome());
		stackPanel.addStyleName("gwt-FullWidthAndHeight");
		
		initWidget(stackPanel);
	}
	
	public abstract String getButtonLabel();
	public abstract void callRemoteAsyncService(final Articolo articolo);
}
